﻿using HandyControl.Controls;
using LpbPrj.Client.Tools.Helper;
using System;
using System.Windows;

namespace LpbPrj.Client.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            if (TouchExtend.HasTouchInput())
            {
                int hwnd = Win32Helper.FindWindow("Shell_TrayWnd", "");
                Win32Helper.ShowWindow(hwnd, Win32Helper.SW_HIDE);

                //this.Topmost = true;
                this.Initialized += CustomWindow_Initialized;
                this.SizeChanged += CustomWindow_SizeChanged;
            }

            InitializeComponent();
        }

        protected override void OnContentRendered(EventArgs e)
        {
            base.OnContentRendered(e);

            NonClientAreaContent = new NonClientAreaContent();
            WindowAttach.SetIgnoreAltF4(this, true);
        }

        private void CustomWindow_Initialized(object sender, EventArgs e)
        {
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;

            var rect = System.Windows.Forms.Screen.PrimaryScreen.Bounds;

            double left = (rect.Width - this.Width) / 2 + rect.Left;
            double top = (rect.Height - this.Height) / 2 + rect.Top;

            this.Left = left;
            this.Top = top;

            System.Diagnostics.Debug.WriteLine($"MainWindow_Initialized rect: W;{rect.Width},H;{rect.Height},L;{rect.Left},T;{rect.Top}");
            System.Diagnostics.Debug.WriteLine($"MainWindow_Initialized this: W;{this.Width},H;{this.Height},L;{this.Left},T;{this.Top}");
        }

        /// <summary>
        /// 大小变化时，重新定位，避免任务栏位置影响界面位置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CustomWindow_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            var rect = System.Windows.Forms.Screen.PrimaryScreen.Bounds;

            double left = (rect.Width - this.ActualWidth) / 2 + rect.Left;
            double top = (rect.Height - this.ActualHeight) / 2 + rect.Top;

            this.Left = left;
            this.Top = top;

            System.Diagnostics.Debug.WriteLine($"MainWindow_SizeChanged rect: W;{rect.Width},H;{rect.Height},L;{rect.Left},T;{rect.Top}");
            System.Diagnostics.Debug.WriteLine($"MainWindow_SizeChanged this: W;{this.ActualWidth},H;{this.ActualHeight},L;{this.Left},T;{this.Top}");
        }

    }
}
